﻿Imports Training.Common

Partial Public Class AddUser
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    ''' <summary>
    ''' Add User Button click
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Run checks for duplicate fields before processing</remarks>
    Private Sub btnAddUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddUser.Click
        If Page.IsValid Then
            ' run checks
            ' check for existing elements of group & membership
            If Len(CheckItems()) <> 0 Then
                ' display error msg
                lblAlert.Text = CheckItems()
            Else
                ' checks passed, proceed with account creation
                lblAlert.Text = CreateUser() ' AddUserFunction
            End If
        Else
            ' display no validation error
            lblAlert.Text = "All fields could not be validated. Please try again."
        End If
    End Sub

    ''' <summary>
    ''' Check login name and email for duplicates
    ''' </summary>
    ''' <returns>Error Message(s)</returns>
    ''' <remarks>use before attempting to create membership account</remarks>
    Private Function CheckItems() As String
        Dim retVal As String

        ' set inital value
        retVal = ""

        ' check username
        If CheckLoginName(tbLogin.Text) > 0 Then
            retVal &= " Login name is in use, please enter a different login name. "
        End If

        ' check email
        'If CheckMemberEmail(tbEmail.Text) > 0 Then
        '    retVal &= " Email address is in use, please enter a different Email address."
        'End If

        Return retVal
    End Function

    ''' <summary>
    ''' Create membership login and add to Users table
    ''' </summary>
    ''' <returns>Returns membership message and error code</returns>
    ''' <remarks>Checks prior to this function should prevent errors</remarks>
    Private Function CreateUser() As String
        Dim retVal As String
        Dim uCount As Integer

        ' create membership account
        retVal = AddGroupLogin(tbLogin.Text, tbPass.Text, tbEmail.Text)

        ' add user to "user" role
        Roles.AddUserToRole(tbLogin.Text, "user")

        ' lookup userid for user to associate login with user table
        Dim user As MembershipUser = Membership.GetUser(tbLogin.Text)
        Dim uid As String = user.ProviderUserKey.ToString()

        ' add user and get iError code
        uCount = AddGroupUser(uid, tbFname.Text, tbLname.Text, ddlGroup.SelectedValue, tbComp.Text, tbAdr.Text, tbCity.Text, ddlState.SelectedValue, tbZip.Text, tbPhone.Text, tbEmail.Text, tbSiteCode.Text, rblActive.SelectedValue)
        If uCount > 0 Then
            retVal &= " Error adding User. Error has been logged."
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Clear all fields and reset values
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Values are not cleared on Add button click, this resets form</remarks>
    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        tbFname.Text = String.Empty
        tbLname.Text = String.Empty
        ddlGroup.SelectedValue = -1
        rblActive.SelectedValue = 1
        tbLogin.Text = String.Empty
        tbPass.Text = String.Empty
        tbPConf.Text = String.Empty
        tbEmail.Text = String.Empty
        tbEConf.Text = String.Empty
        lblAlert.Text = String.Empty
    End Sub
End Class